#include <iostream>
#include <string>
#include <vector>
#include <utility>

#include "dbmock.h"
#include "parser.h"
#include "protocol.h"

using namespace std;

int main() {
	dbmock *mock = new dbmock();
	Parser p(mock);
	
	vector<unsigned char> input;
	
	//3. Delete a newsgroup. The identiﬁcation number of the group is sent as a parameter.
	//COM_DELETE_NG num_p COM_END
	//ANS_DELETE_NG [ANS_ACK | ANS_NAK ERR_NG_DOES_NOT_EXIST] ANS_END
	input.push_back(Protocol::COM_DELETE_NG);
	p.push_int(input,0);
	input.push_back(Protocol::COM_END);

	vector<unsigned char> output = p.handle_input(input);

	for(unsigned char c : output) {
		string s = p.translate(c);
		cout << s << ", ";
	}
	cout << endl << endl;

	//1. List newsgroups. The reply contains the number of newsgroups followed by the identiﬁcation numbers and titles of 		//	the groups.
	//COM_LIST_NG COM_END
	//ANS_LIST_NG num_p [num_p string_p]* ANS_END
	
	input.clear();
	input.push_back(Protocol::COM_LIST_NG);
	input.push_back(Protocol::COM_END);
	output = p.handle_input(input);
	unsigned int pos = 0;
	cout << p.translate(output[pos]) << ", "; //ANS_LIST_NG
	pos++;
	cout << p.translate(output[pos]) << ", "; //PAR_NUM
	unsigned int nbr = p.capture_int(output, pos); //N
	cout << nbr << ", " << endl;
	for(unsigned int i = 0; i != nbr; ++i) {
		cout << p.translate(output[pos]) << ", "; //PAR_NUM
		unsigned int id = p.capture_int(output, pos); //N
		cout << id << ", ";
		cout << p.translate(output[pos]) << ", "; //PAR_STRING
		string title = p.capture_string(output,pos); //Title
		cout << title << endl;
	}
	cout << p.translate(output[pos]) << endl << endl; //ANS_END

	//2. Create a newsgroup. The title of the group is sent as a parameter.
	//COM_CREATE_NG string_p COM_END
	//ANS_CREATE_NG [ANS_ACK | ANS_NAK ERR_NG_ALREADY_EXISTS] ANS_END
	input.clear();
	input.push_back(Protocol::COM_CREATE_NG);
	string name = "bla";
	p.push_string(input,name);
	input.push_back(Protocol::COM_END);
	output = p.handle_input(input);
	for(unsigned char c : output) {
		string s = p.translate(c);
		cout << s << ", ";
	}
	cout << endl << endl;



	delete mock;
}
